﻿//=============================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：江怀玉
// 日 期：2011/10/27
// 描 述: 部门业务处理类
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/11/1     江怀玉           
//=============================================================

using System;
using System.Collections.Generic;
using System.Linq;
using iSun.Entity;

namespace iSun.BLL
{
    public partial class BDept : BLL.Base.BaseBussnies
    {
        public BDept() {
            this.entity = new DatDepartment();
        }

        /// <summary>
        /// 获取该部门下所有子部门信息
        /// </summary>
        /// <param name="deptPath">部门路径</param>
        /// <returns>IList：DatDepartment</returns>
        public IList<DatDepartment> GetDeptListByDeptPath(string deptPath)
        {
            return this.GetEntitysByCondition<DatDepartment>(string.Format("DeptPath LIKE '{0}%'",deptPath));
        }

         /// <summary>
         /// 获取该部门下所有子总门信息
         /// </summary>
         /// <returns></returns>
        public IList<DatDepartment> GetDeptListByDeptID(Guid? DeptID)
        {
            return this.GetEntitysBySqlComm<DatDepartment>(string.Format(@"
            select a.* from DatDepartment a,[dbo].[fn_GetOrganize]('{0}') b
            where a.DeptID = b.id
            ",DeptID)); 
        }

        
        /// <summary>
        /// 批量更新部门树编码
        /// </summary>
        /// <param name="DeptID">部门ID</param>
        /// <returns></returns>
        public bool BatchUpdateDeptTreeCode(Guid DeptID)
        {
            IList<DatDepartment> _list = GetDeptListByDeptID(DeptID);
            foreach (var entity in _list)
            {
                entity.DeptPath = null;
                this.Update(entity);
            }
            BatchUpdateDeptTreeCode(DeptID, _list);
            return true ;
        }

        /// <summary>
        /// 批量更新部门树编码
        /// </summary>
        /// <param name="DeptID">部门ID</param>
        /// <param name="list">要更新部门列表</param>
        /// <returns></returns>
        public bool BatchUpdateDeptTreeCode(Guid DeptID,IList<DatDepartment> list)
        {
            if (list == null)
            {
                return false;
            }
            var _list = list.Where(p => p.ParentID == DeptID).OrderBy(p=>p.Sort);
            if (_list == null)
            {
                return false;
            }
            foreach (var entity in _list)
            {
                entity.DeptPath = CreateNewTreeCode(DeptID);
                this.Update(entity);
                BatchUpdateDeptTreeCode(entity.ID, list);
            }
            return true;
        }

        /// <summary>
        /// 部门移动
        /// </summary>
        /// <param name="curDeptID">当前部门</param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public bool DeptMove(Guid curDeptID, Guid ParentID)
        {
           DatDepartment curDept  = GetEntityByCondition<DatDepartment>("DeptID='"+curDeptID+"'");
           curDept.ParentID = ParentID;
           this.entity = curDept;
           this.Update();
           BatchUpdateDeptTreeCode(curDept.ID);
           return true;
        }


        /// <summary>
        /// 部门复制
        /// </summary>
        /// <param name="curDeptID"></param>
        /// <returns></returns>
        public bool DeptCopy(Guid curDeptID)
        {
            DatDepartment curDept = GetEntityByCondition<DatDepartment>("DeptID='" + curDeptID + "'");
            ///要复制部门数据
            IList<DatDepartment> list = GetDeptListByDeptID(curDeptID);
            Guid SourceID = curDeptID;
            curDept.ID = Guid.NewGuid();
            curDept.Name = curDept.Name + "1";
            this.entity = curDept;
            this.Insert(entity);
            DeptCopy(SourceID, curDept.ID, list);
            BatchUpdateDeptTreeCode(curDept.ID);
            return true;
        }

        /// <summary>
        /// 部门复制
        /// </summary>
        /// <param name="sourceID"></param>
        /// <param name="newID"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool DeptCopy(Guid sourceID,Guid newID,IList<DatDepartment> list)
        {
            var _list = list.Where(p => p.ParentID == sourceID);
            foreach(var  entity in _list)
            {
                entity.ParentID = newID;
                Guid _sourceID = entity.ID;
                entity.ID = Guid.NewGuid();
                this.Insert(entity);
                DeptCopy(_sourceID, entity.ID, list);
            }
            return true;
        }


        /// <summary>                                             
        /// 创建TreeCode
        /// </summary>
        /// <param name="parentID">父ID</param>
        /// <returns>新CodePath</returns>
        public string CreateNewTreeCode(Guid parentID)
        {
            DatDepartment pDept= this.GetEntityById(parentID.ToString()) as DatDepartment;
            IList<DatDepartment> deptList = this.GetEntitysByCondition<DatDepartment>("ParentID='" + parentID + "'");
            if (deptList == null)
            {
                return pDept.DeptPath + "-001";
            }
            else
            {
                string bDept = deptList.Max(p => p.DeptPath);
                if (bDept == string.Empty || bDept.IndexOf("-") == -1)
                {
                    return pDept.DeptPath + "-001";
                }
                 string[] bDepts = bDept.Split('-');
                return pDept.DeptPath + "-" + bDepts[bDepts.Length - 1].Replace(System.Int32.Parse(bDepts[bDepts.Length - 1]).ToString().Replace('@', ' '), "") + (System.Int32.Parse(bDepts[bDepts.Length - 1]) + 1);
            }
               
        }
    }
}
